package com.demo.esjd.service.impl;

import com.alibaba.fastjson.JSON;
import com.demo.esjd.pojo.Content;
import com.demo.esjd.service.ContentService;
import com.demo.esjd.utils.HtmlParseUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.Highlighter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Autowired
    private HtmlParseUtil htmlParseUtil;

    public  static  void  main(String[] arg) throws IOException {
        new ContentServiceImpl().parseContent("java");
    }

    @Override
    public  boolean parseContent(String keyword) throws IOException {
        List<Content> contentList = htmlParseUtil.parseJD(keyword);
        BulkRequest bulkRequest = new BulkRequest();
        for (int i = 0; i < contentList.size(); i++) {
            bulkRequest.add(
                    new IndexRequest("goods").source(
                            JSON.toJSONString(contentList.get(i)),
                            XContentType.JSON

              ));
        }

        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);

        return  !bulk.hasFailures();
    }

    @Override
    public  List<Map<String,Object>> searchPage(String keyword,int pageNo,int pageSize) throws IOException {
        if (pageNo<=1){
            pageNo=1;
        }

        SearchRequest searchRequest = new SearchRequest("goods");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        searchSourceBuilder.from(pageNo);
        searchSourceBuilder.size(pageSize);

        TermQueryBuilder builder = QueryBuilders.termQuery("name", keyword);
        searchSourceBuilder.query(builder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        ArrayList<Map<String, Object>> mapArrayList = new ArrayList<>();
        for (SearchHit documentFields : search.getHits().getHits()) {
            mapArrayList.add(documentFields.getSourceAsMap());
        }

        return  mapArrayList;
    }

    @Override
    public  List<Map<String,Object>> searchPageHightLight(String keyword,int pageNo,int pageSize) throws IOException {
        if (pageNo<=1){
            pageNo=1;
        }

        //高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("name");
        highlightBuilder.requireFieldMatch(false);
        highlightBuilder.preTags("<span style='color:red'");
        highlightBuilder.postTags("</span>");


        SearchRequest searchRequest = new SearchRequest("goods");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        searchSourceBuilder.from(pageNo);
        searchSourceBuilder.size(pageSize);
        searchSourceBuilder.highlighter(highlightBuilder);

        TermQueryBuilder builder = QueryBuilders.termQuery("name", keyword);

        searchSourceBuilder.query(builder);
        searchRequest.source(searchSourceBuilder);



        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);


        ArrayList<Map<String, Object>> mapArrayList = new ArrayList<>();
        for (SearchHit documentFields : search.getHits().getHits()) {

            Map<String, HighlightField> highlightFields = documentFields.getHighlightFields();

            HighlightField name = highlightFields.get("name");
            Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();

            if (name!=null){
                Text[] fragments = name.fragments();

                String n_name="";
                for (Text fragment : fragments) {
                    n_name+=fragment;
                }
                sourceAsMap.put("name",n_name);
            }

            mapArrayList.add(sourceAsMap);
        }

        return  mapArrayList;
    }
}
